package com.lp.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.domain.order.Order;
import org.iframework.commons.domain.order.OrderImpl;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.domain.pager.PagerImpl;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.controller.BaseControllerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lp.business.domain.Result;
import com.lp.business.function.DownloadUtil;
import com.lp.business.model.Download;
import com.lp.business.model.Member;
import com.lp.business.model.Report;
import com.lp.business.service.DownloadService;
import com.lp.business.service.MemberService;
import com.lp.business.service.ReportService;
import com.lp.service.conf.Conf;

/**
 * 收藏
 * 
 * @author lipeng
 * @date 2018年08月22日 下午1:15:45
 */
@RestController
@RequestMapping("/api/report")
public class DownloadController extends BaseControllerSupport {

	@Autowired
	private ReportService reportService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private DownloadService downloadService;

	/**
	 * 下载报告 <br>
	 */
	@RequestMapping(value = "download/{reportId}", method = { RequestMethod.POST, RequestMethod.GET })
	public Result download(HttpServletRequest request, final HttpServletResponse response,@PathVariable String reportId) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			if (V.isEmpty(reportId)) {
				return Result.error("报告id未传入");
			}
			Report report = reportService.get(reportId);
			if (V.isEmpty(report)) {
				return Result.error("报告id非法，收藏失败");
			}
			String filePath = report.getFilePath();
			String filename = filePath.substring(filePath.lastIndexOf("/")+1, filePath.length());
			String path = Conf.IMG_BASE_RES+filePath;
			DownloadUtil.outputFile(response, filename, path, true, true, true);
			return Result.ok();
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("下载失败");
		}

	}
	
	public static void main(String[] args) {
		String filePath = "shujuju/3931.pdf";
		String filename = filePath.substring(filePath.lastIndexOf("/")+1, filePath.length());
		System.out.println("filename:"+filename);
	}

	@RequestMapping(value = "download2", method = { RequestMethod.POST, RequestMethod.GET })
	public Result download2(HttpServletRequest request, final HttpServletResponse response) {
		try {
			String filePath = "http://ftp.shujuju.cn/platform/file/2018-01-27/2018%E5%B9%B4%E8%9E%8D%E5%90%88%E5%9C%BA%E6%99%AF%E4%B8%8B%E7%9A%84%E4%BA%92%E8%81%94%E7%BD%91%E5%95%86%E4%B8%9A%E4%BB%B7%E5%80%BC%E7%A0%94%E7%A9%B6%E6%8A%A5%E5%91%8A.pdf";
			// String filePath = "C://Users//DELL//Pictures//3.jpg";
			String filename = "3.pdf";
			// // // 方法1：IO流实现下载的功能
			// // output1(response, filename,filePath);
			// // // 方法1-2：IO字符流下载，用于大文件
			// // output2(response, filename,filePath);
			// //DownloadUtil.outputFile(response, filename, filePath, true);
			//// // 方法1-3：将附件中多个文件进行压缩，批量打包下载文件 //创建压缩文件需要的空的zip包 String
			//// String filePath1 = "C://Users//DELL//Pictures//1.jpg";
			//// String filePath2 = "C://Users//DELL//Pictures//2.jpg";
			//// String filePath3 = "C://Users//DELL//Pictures//3.jpg";
			//// // 创建需要下载的文件路径的集合
			//// List<String> filePaths = new ArrayList<String>();
			//// filePaths.add(filePath1);
			//// filePaths.add(filePath2);
			//// filePaths.add(filePath3);
			//// String filename = "temp.zip";
			//// String zipFilePath = "C://Users//DELL//Pictures" + File.separator +
			// filename;
			//// DownloadUtil.outputZip(response, filename, zipFilePath, filePaths);
			//
			DownloadUtil.outputFile(response, filename, filePath, true, true, true);
			// String filePath =
			// "http://ftp.shujuju.cn/platform/file/2018-01-27/2018%E5%B9%B4%E8%9E%8D%E5%90%88%E5%9C%BA%E6%99%AF%E4%B8%8B%E7%9A%84%E4%BA%92%E8%81%94%E7%BD%91%E5%95%86%E4%B8%9A%E4%BB%B7%E5%80%BC%E7%A0%94%E7%A9%B6%E6%8A%A5%E5%91%8A.pdf";
			// downLoad(filePath,response, true);
			return Result.ok();
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("下载失败");
		}
	}
	
	/**
	 * 获取收藏列表 <br>
	 */
	@RequestMapping(value = "download/list", method = { RequestMethod.POST, RequestMethod.GET })
	public Result list(HttpServletRequest request, final HttpServletResponse response) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			Download model = new Download();
			model.setMemberId(member.getId());
			model.setStatus(RecordStatus.AVAILABLE);
			Order order = new OrderImpl(request);
			Pager pager = new PagerImpl(request);
			List<Map<String, Object>> downloads = downloadService.findDownloads(model, order, pager);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("downloads", downloads);
			return Result.ok().put("data", data);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
}
